package com.aitaoxue.palx.ui.register;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.InjectView;

import com.aitaoxue.palx.AppManager;
import com.aitaoxue.palx.R;
import com.aitaoxue.palx.api.remote.AitaoXueApi;
import com.aitaoxue.palx.base.BaseActivity;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class ForgetPasswordActivity extends BaseActivity {

	@InjectView(R.id.phone)
	EditText phone;
	@InjectView(R.id.checkNum)
	EditText checkNum;
	@InjectView(R.id.btn_check)
	Button check;
	@InjectView(R.id.btn_push)
	Button push;
	@InjectView(R.id.alert_line)
	RelativeLayout alertLayout;
	@InjectView(R.id.alert_content)
	TextView alertContent;

	private CountDownTimer counter;

	@Override
	protected boolean hasBackButton() {
		return true;
	}

	@Override
	protected int getActionBarTitle() {
		return R.string.forget_password;
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_forget_password;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_check:
			if (TextUtils.isEmpty(phone.getText())) {
				showToast("请输入手机号", 0, 0);
				return;
			}

			showWaitDialog();
			AitaoXueApi.prodFindPsw1(phone.getText().toString(), mHandler);
			break;
		case R.id.btn_push:
			if (TextUtils.isEmpty(checkNum.getText())) {
				showToast("请输入验证码", 0, 0);
				return;
			}

			showWaitDialog();
			AitaoXueApi.prodFindPsw2(checkNum.getText().toString(), mHandler2);
			break;
		default:
			break;
		}
	}

	@Override
	public void initView() {
		check.setOnClickListener(this);
		push.setOnClickListener(this);
		
		AppManager.getAppManager().addActivity(this);
	}

	@Override
	public void initData() {

	}

	private AsyncHttpResponseHandler mHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {
			
		}

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			hideWaitDialog();
			String res = new String(arg2);

			try {
				JSONObject json = new JSONObject(res);
				String code = json.getString("code");

				if (code.equals("0000")) {
					showToast("验证码已发送", 0, 0);
					trimCounting();
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	};

	private AsyncHttpResponseHandler mHandler2 = new AsyncHttpResponseHandler() {

		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			hideWaitDialog();
			String res = new String(arg2);

			try {
				JSONObject json = new JSONObject(res);
				String code = json.getString("code");

				if (code.equals("0000")) {
					Intent intent=new Intent(ForgetPasswordActivity.this,
							ForgetPasswordActivity2.class);
					intent.putExtra("from", "forget");
					startActivity(intent);
					finish();
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {

		}
	};

	private void trimCounting() {
		check.setEnabled(false);
		counter = new CountDownTimer(60 * 1000, 1000) {

			@Override
			public void onTick(long millisUntilFinished) {
				check.setText(millisUntilFinished / 1000 + "");
			}

			@Override
			public void onFinish() {
				check.setEnabled(true);
				check.setText("获取验证码");
				counter.cancel();
			}
		};

		counter.start();
	}
}
